<template>
  <RbtDialog
    v-model:visible="dialogVisible"
    :title="`${formData.id ? '编辑' : '添加'}收货地址`"
  >
    <div class="address-edit">
      <div class="rbt-form">
        <div class="rbt-form-item">
          <div class="label">收货人:</div>
          <div class="field">
            <input
              v-model="formData.receiver"
              class="input"
              placeholder="请输入收货人"
            />
          </div>
        </div>
        <div class="rbt-form-item">
          <div class="label">手机号:</div>
          <div class="field">
            <input
              v-model="formData.contact"
              class="input"
              placeholder="请输入手机号"
            />
          </div>
        </div>
        <div class="rbt-form-item">
          <div class="label">地区:</div>
          <div class="field">
            <RbtCity
              :fullLocation="formData.fullLocation"
              placeholder="请选择所在地区"
              @change="changeCity"
            />
          </div>
        </div>
        <div class="rbt-form-item">
          <div class="label">详细地址:</div>
          <div class="field">
            <input
              v-model="formData.address"
              class="input"
              placeholder="请输入详细地址"
            />
          </div>
        </div>
        <div class="rbt-form-item">
          <div class="label">邮政编码:</div>
          <div class="field">
            <input
              v-model="formData.postalCode"
              class="input"
              placeholder="请输入邮政编码"
            />
          </div>
        </div>
        <div class="rbt-form-item">
          <div class="label">地址标签:</div>
          <div class="field">
            <input
              v-model="formData.addressTags"
              class="input"
              placeholder="请输入地址标签,逗号隔开"
            />
          </div>
        </div>
      </div>
    </div>
    <template #footer>
      <RbtButton type="gray" style="margin-right: 20px">取消</RbtButton>
      <RbtButton type="primary" @click="submit">确定</RbtButton>
    </template>
  </RbtDialog>
</template>

<script setup lang="ts">
import { ResultType } from '@/types/goods'
import { OrderUserAddressType } from '@/types/user'
import { addAddress, editAddress } from '@/api/order'
import Message from '@/components/Message'

const emits = defineEmits<{
  (e: 'on-success', formData: OrderUserAddressType): void
}>()

const dialogVisible = ref(false)
// 打开函数
const open = (form: OrderUserAddressType) => {
  // 先填充数据 - 编辑
  if (form.id) {
    // 传入{}的时候就是清空 否则就是赋值
    for (const key in formData) {
      formData[key] = form[key]
    }
  } else {
    // 先清空数据 - 添加
    for (const key in formData) {
      if (key !== 'isDefault') {
        formData[key] = ''
      }
    }
  }
  dialogVisible.value = true
}

// 表单数据
const formData = reactive<OrderUserAddressType>({
  id: '',
  receiver: '',
  contact: '',
  provinceCode: '',
  cityCode: '',
  countyCode: '',
  address: '',
  postalCode: '',
  addressTags: '',
  isDefault: 0,
  fullLocation: ''
})

// 选择地区
const changeCity = (data: ResultType) => {
  formData.provinceCode = data.provinceCode
  formData.cityCode = data.cityCode
  formData.countyCode = data.countryCode
  formData.fullLocation = data.fullLocation
}

// 提交操作
const submit = () => {
  if (formData.id) {
    editAddress(formData).then(() => {
      // 修改成功
      Message({ text: '修改收货地址成功', type: 'success' })
      dialogVisible.value = false
      emits('on-success', formData)
    })
  } else {
    addAddress(formData).then(res => {
      // 添加成功
      Message({ text: '添加收货地址成功', type: 'success' })
      formData.id = res.data.result.id
      dialogVisible.value = false
      emits('on-success', formData)
    })
  }
}

// 向外暴露方法或者数据
defineExpose({ open })
</script>

<style scoped lang="scss">
.rbt-dialog {
  :deep(.wrapper) {
    width: 780px;
    .body {
      font-size: 14px;
    }
  }
}
.rbt-form {
  padding: 0;
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
}
.rbt-city {
  width: 320px;
  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}
</style>
